from django.db import models
from django.contrib.auth.models import User

class Stock(models.Model):
    code = models.CharField(max_length=10, unique=True, verbose_name="股票代码")
    name = models.CharField(max_length=50, verbose_name="股票名称")
    last_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, verbose_name="最新价格")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return f"{self.code} - {self.name}"

class UserStock(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    stock = models.ForeignKey(Stock, on_delete=models.CASCADE, verbose_name="股票")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="关注时间")

    class Meta:
        unique_together = ('user', 'stock')

class TradingSignal(models.Model):
    SIGNAL_TYPES = (
        ('BUY', '买入'),
        ('SELL', '卖出'),
        ('HOLD', '持有'),
    )

    stock = models.ForeignKey(Stock, on_delete=models.CASCADE, verbose_name="股票")
    signal_type = models.CharField(max_length=10, choices=SIGNAL_TYPES, verbose_name="信号类型")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="信号价格")
    reason = models.TextField(verbose_name="信号原因")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间") 